﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Merise_designer.Donnees
{
    public class CadTools
    {
        private CL_CAD cad = new CL_CAD();

        //Retourne le numéro auto le plus élevé dans la base
        public int getLastNumeroAuto(string monJeu)
        {
            DataRow row;
            string[] id = new string[3];

            //Entités
            string req = @"SELECT ID_ENTITE FROM ENTITES ORDER BY ID_ENTITE DESC LIMIT 1";
            try
            {
                row = cad.GetRows(req, monJeu).Tables[monJeu].Rows[0];
                StringBuilder sb = new StringBuilder();
                foreach (object obj in row.ItemArray)
                {
                    sb.Append(obj.ToString());
                }
                id[0] = sb.ToString();
            }
            catch { }

            //Associations
            req = @"SELECT ID_ASSO FROM ASSOCIATIONS ORDER BY ID_ASSO DESC LIMIT 1";
            try
            {
                row = cad.GetRows(req, monJeu).Tables[monJeu].Rows[0];
                StringBuilder sb1 = new StringBuilder();
                foreach (object obj in row.ItemArray)
                {
                    sb1.Append(obj.ToString());
                }
                id[1] = sb1.ToString();
            }
            catch { }

            //Lignes
            req = @"SELECT ID_LIGNE FROM LIGNES ORDER BY ID_LIGNE DESC LIMIT 1";
            try
            {
                row = cad.GetRows(req, monJeu).Tables[monJeu].Rows[0];
                StringBuilder sb2 = new StringBuilder();
                foreach (object obj in row.ItemArray)
                {
                    sb2.Append(obj.ToString());
                }
                id[2] = sb2.ToString();
            }
            catch { }


            //Récupère l'élément le plus grand
            int idResult;
            try
            {
                var idOrdered = id.OrderByDescending(n => n).ToList(); //tris dans l'ordre décroissant
                idResult = Convert.ToInt32(idOrdered[0].Replace(Methodes.CL_Variables.getPrefixNameAuto, null));
            }
            catch
            {
                idResult = 0;
            }

            return idResult;
        }

        /// <summary>
        /// Retourne le texte formaté pour enregistrer la position d'une entité ou d'une association dans la BDD
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String getPosition(Object obj)
        {
            if(obj.GetType()== typeof(Objets.CL_Entite))
            {
                return ((Objets.CL_Entite)obj).posLeft + ";" + ((Objets.CL_Entite)obj).posTop;
            }

            if (obj.GetType() == typeof(Objets.CL_Association))
            {
                return ((Objets.CL_Association)obj).posLeft + ";" + ((Objets.CL_Association)obj).posTop;
            }
            return "";
        }

        /// <summary>
        /// Créé une nouvelle BDD
        /// </summary>
        /// <param name="bddFileName"></param>
        /// <returns></returns>
        public Boolean generateNewBDD(String bddFileName,String reqInsert)
        {

            String req =
                "BEGIN TRANSACTION;" +
                "CREATE TABLE ASSOCIATIONS (ID_ASSO TEXT, NOM_ASSO TEXT, CODE_ASSO TEXT, LARG_ASSO NUMERIC, HAUT_ASSO NUMERIC, POSITION_ASSO TEXT);" +
                "CREATE TABLE DICTIONNAIRE (ID_DIC INTEGER PRIMARY KEY, NOM_DIC TEXT, CODE_DIC TEXT, TYPE_DIC NUMERIC, ID_ASSO TEXT, ID_ENTITE TEXT, PK NUMERIC,REQ NUMERIC);" +
                "CREATE TABLE ENTITES (ID_ENTITE TEXT, NOM_ENTITE TEXT, CODE_ENTITE , LARG_ENTITE NUMERIC, HAUT_ENTITE NUMERIC, POSITION_ENTITE TEXT);" +
                "CREATE TABLE LIGNES (ID_ENTITE TEXT, ID_LIGNE TEXT, ID_ASSO TEXT, CARDINALITE TEXT);" +
                "CREATE TABLE TYPES (NOM_TYPE TEXT, ID_TYPE INTEGER PRIMARY KEY);" +
                "INSERT INTO TYPES VALUES('BOOL',1);" +
                "INSERT INTO TYPES VALUES('AUTO_INCREMENT',2);" +
                "INSERT INTO TYPES VALUES('INT',3);" +
                "INSERT INTO TYPES VALUES('TEXT',4);" +
                "INSERT INTO TYPES VALUES('NUMERIC',5);" +
                "INSERT INTO TYPES VALUES('DATE',6);" +
                "CREATE TABLE WORKSPACE (ID_WORKSPACE INTEGER PRIMARY KEY, ZOOM TEXT, POSITION TEXT);" +
                reqInsert+
                "COMMIT;";
            return cad.CreateBDD(bddFileName,req, true) == 1;
        }
    }
}
